#include "genresult.h"

GenResult::GenResult(QVector<int> vResults, QWidget *parent)
    : QWidget(parent), vResults(vResults)
{
    labels[0]->setStyleSheet("color:rgb(120,120,120)");
    labels[1]->setStyleSheet("background-color:rgb(250,250,250)");
    labels[2]->setStyleSheet("color:rgb(120,120,120)");
    SetPointSize(labels[1], 11);
    labels[0]->setVisible(false);
    labels[2]->setVisible(false);
    connect(cbbShowLeft, &QCheckBox::stateChanged, [this](bool state) { labels[0]->setVisible(state); });
    connect(cbbShowRight, &QCheckBox::stateChanged, [this](bool state) { labels[2]->setVisible(state); });

    btnPrev->setFocusPolicy(Qt::NoFocus);
    btnNext->setFocusPolicy(Qt::NoFocus);
    connect(btnPrev, &QPushButton::clicked, [this]{ updateLabel(current - 1); });
    connect(btnNext, &QPushButton::clicked, [this]{ updateLabel(current + 1); });

    btnShowAll->setFocusPolicy(Qt::NoFocus);
    btnClose->setFocusPolicy(Qt::NoFocus);
    connect(btnShowAll, &QPushButton::clicked, this, &GenResult::onShowAll);
    connect(btnClose, &QPushButton::clicked, this, &GenResult::close);

    //创建布局
    QHBoxLayout *layCbb = new QHBoxLayout;
    layCbb->addStretch();
    layCbb->addWidget(cbbShowLeft);
    layCbb->addSpacing(16);
    layCbb->addWidget(cbbShowRight);
    layCbb->addStretch();

    QHBoxLayout *layLabels = new QHBoxLayout;
    for(QLabel *label : labels) {
        label->setAlignment(Qt::AlignCenter | Qt::AlignVCenter);
        LimitSize(label, QSize(80, 40));
        QHBoxLayout *layLabel = new QHBoxLayout;
        layLabel->setMargin(0);
        layLabel->addWidget(label);
        QWidget *widget = new QWidget;
        LimitSize(widget, QSize(80, 40));
        widget->setLayout(layLabel);
        layLabels->addWidget(widget);
    }

    QHBoxLayout *layBtns = new QHBoxLayout;
    layBtns->addStretch();
    layBtns->addWidget(btnPrev);
    layBtns->addSpacing(16);
    layBtns->addWidget(btnNext);
    layBtns->addStretch();

    QHBoxLayout *layBottom = new QHBoxLayout;
    layBottom->addWidget(btnShowAll);
    layBottom->addStretch();
    layBottom->addWidget(btnClose);

    QVBoxLayout *layCentral = new QVBoxLayout;
    layCentral->addLayout(layCbb);
    layCentral->addSpacing(10);
    layCentral->addWidget(labelCurrentIndex, 0, Qt::AlignCenter);
    layCentral->addLayout(layLabels);
    layCentral->addLayout(layBtns);

    QGroupBox *box = new QGroupBox("抽取结果");
    box->setLayout(layCentral);

    QVBoxLayout *layMain = new QVBoxLayout;
    layMain->addWidget(box);
    layMain->addLayout(layBottom);
    setLayout(layMain);
    adjustSize();
    LimitSize(this, size());
    setWindowTitle("结果");

    updateLabel(0);
}

void GenResult::updateLabel(int index) {
    if(index < 0 || index >= vResults.size())
        return;
    current = index;
    labelCurrentIndex->setText("第 " + QString::number(index + 1) + " 个");
    for(int i = 0; i < 3; i++) {
        QLabel *label = labels[i];
        int offsetedIndex = index - 1 + i;
        if(offsetedIndex < 0 || offsetedIndex >= vResults.size()) {
            label->setText("");
        } else {
            label->setText(QString::number(vResults[offsetedIndex]));
        }
    }
}

void GenResult::onShowAll() {
    QListWidget *listWidget = new QListWidget;

    int row = 1;
    for(int result : vResults) {
        QListWidgetItem *item = new QListWidgetItem("第" + QString::number(row) + "个:\t" + QString::number(result));
        listWidget->addItem(item);
        row++;
    }

    listWidget->resize(300, 500);
    AdjustPosition(this, listWidget);
    listWidget->setAttribute(Qt::WA_DeleteOnClose);
    listWidget->show();
}
